﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ShopB2C.Entity;
using ShopB2C.Helper;
using ShopB2C.Parameters;
using ShopB2C.ViewModels;

namespace ShopB2C.Controllers.Manager {
    /// <summary>
    /// PropertyVal
    /// </summary>
    public partial class ClassifyController : BaseController {

        public ActionResult PropertyValList(string classifyParams) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            if (cp.PropertyId == 0) {
                throw new Exception("请选择分类特性");
            }
            ClassifyPropertyEntity cpe = _classify.GetPropertyById(cp.PropertyId);

            this.SiteMapPathAdd("分类特性值");

            int pageCount;
            List<ClassifyPropertyValEntity> cpveList = _classify.GetPropertyValByProperty(cp.PropertyId,
                this.PageSize, cp.PropertyValPageNumber, out pageCount);

            ClassifyParam cpPager = new ClassifyParam() {
                ModuleKey = cp.ModuleKey,
                ParentClassifyId = cp.ParentClassifyId,
                ClassifyId = cp.ClassifyId,
                ClassifyPageNumber = cp.ClassifyPageNumber,
                PropertyId = cp.PropertyId,
                PropertyPageNumber = cp.PropertyPageNumber,
                PropertyValPageNumber = 1234554321
            };
            object pager = this.CreatePager(Url.Action("PropertyValList", new {
                classifyParams = cpPager.ToString().Replace("1234554321", "{0}")
            }), pageCount, cp.PropertyValPageNumber);

            cp.AutoSequence = cpveList != null
                ? this.PageSize * (cp.PropertyValPageNumber - 1) + cpveList.Count + 1
                : 1;

            return View("PropertyValList",
                new ClassifyPropertyValViewModel() {
                    PropertyName = cpe.Name,
                    ClassifyParams = cp,
                    ClassifyPropertyValList = cpveList,
                    Pager = pager
                });
        }

        public ActionResult PropertyValEdit(string classifyParams) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            ClassifyPropertyEntity cpe = _classify.GetPropertyById(cp.PropertyId);

            this.SiteMapPathAdd("分类特性值", Url.Action("PropertyValList", new { classifyParams = cp.ToString() }));
            this.SiteMapPathAdd("分类特性值编辑");

            ClassifyPropertyValEntity cpve = cp.PropertyValId == 0
                ? new ClassifyPropertyValEntity() { Sequence = cp.AutoSequence }
                : _classify.GetPropertyValById(cp.PropertyValId);

            cp.AutoSequence = 0;
            cp.PropertyValId = 0;
            return View(new ClassifyPropertyValViewModel() {
                PropertyName = cpe.Name,
                ClassifyParams = cp,
                ClassifyPropertyVal = cpve
            });
        }

        [HttpPost]
        public ActionResult PropertyValEdit(string classifyParams, ClassifyPropertyValViewModel cpvvm) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            ClassifyPropertyValEntity cpve = cpvvm.ClassifyPropertyVal;

            cpve.PropertyId = cp.PropertyId;
            if (cpve.PropertyValId == 0) {
                _classify.PropertyValAppend(cpve);
                cp.AutoSequence++;
            }
            else {
                _classify.PropertyValUpdate(cpve);
                cp.AutoSequence = 0;
                cp.PropertyValId = 0;
            }
            return this.RedirectToAction(
                cpve.PropertyValId == 0 ? "PropertyValEdit" : "PropertyValList",
                new { classifyParams = cp.ToString() });
        }

        public ActionResult PropertyValDelete(string classifyParams) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            _classify.PropertyValDelete(cp.DeleteIds);

            cp.PropertyValId = 0;
            cp.DeleteIds = "0";
            return RedirectToAction("PropertyValList", new { classifyParams = cp.ToString() });
        }

        [HttpPost]
        public ActionResult PropertyValEditBatchSeq(string classifyParams, FormCollection fc) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            _classify.PropertyValBatchUpdateSequence(fc["cbKey"], fc["tbSequence"]);

            cp.PropertyValId = 0;
            return this.RedirectToAction("PropertyValList", new { classifyParams = cp.ToString() });
        }

    }
}